<% @page_title = 'Returning book' %>
<%= stylesheet_link_tag    "admin/borrow-return", :media => "all" %>
<%= stylesheet_link_tag    "admin/list-items", :media => "all" %>

<h1>Returning book</h1>
<div id='return-book'>
	<div class='check-reader'>
		<%= form_tag(:controller => 'admin/book_borrow', :action => 'check_reader', :type => 'return') do %>
			<p class='dif'>
				<label class='dif'>Card ID</label>
				<%= text_field_tag 'card_id' %>
				<%= submit_tag 'Check', :name => 'check_reader' %>
			</p>
		<% end %><!-- End form -->
		<div class='reader-info'>
			<% if session[:reader] %>
				<% @reader = session[:reader] %>
				<p><label>Full Name:</label><%= @reader.fullname %></p>
				<p><label>Faculty:</label><%= Faculty.getFacultyName(@reader.faculty_id) %></p>
				<p><label>Reader Group:</label><%= Role.getRoleName(@reader.role_id) %></p>
				<p><label>Created Time:</label><%= @reader.created_at %></p>
			<% end %>
		</div><!-- End .reader-info -->
		<% if flash[:reader_notice] != nil %>
			<div class='notice'>
				<h3><%= flash[:reader_notice] %></h3>
			</div>
		<% end %>
	</div><!-- End .check-reader -->
	<div class='return-book'>
		<%= form_tag(:controller => 'admin/book_borrow', :action => 'return_book', :type => 'return') do %>
			<p class='dif'>
				<label class='dif'>Book ID</label>
				<%= text_field_tag 'book_id' %>
				<%= submit_tag 'Return', :name => 'return_book' %>
				<%= submit_tag 'Extend', :name => 'extend_time' %>
			</p>
		<% end %><!-- End form -->	
		<div class='book-info'>
			<% if session[:book] %>
				<% @book = session[:book] %>
				<p><label>Title:</label><%= @book.title %></p>
				<p><label>Author:</label><%= @book.author %></p>
				<p><label>Category:</label><%= BookCategory.getBookCateName(@book.book_category_id) %></p>
				<p><label>Created Time:</label><%= @book.created_at %></p>
			<% end %>
		</div><!-- End .reader-info -->
		<% if flash[:book_notice] != nil %>
			<div class='notice'>
				<h3><%= flash[:book_notice] %></h3>
			</div>
		<% end %>
	</div><!-- End .check-book -->
	<div class='clear'></div>
	
	<div class='list-borrowing-book'>
		<% if session[:borrowing_books] and session[:borrowing_books].size > 0 %>
			<table>
				<tr>
					<th>Book Name</th>
					<th>Book ID</th>
					<th>Borrowed Time</th>
					<th>Return Time</th>
				</tr>
			
				<% @borrowing_books = session[:borrowing_books] %>
				<% order = 0 %>
				<% for borrow_book in @borrowing_books %>
				<tr <% if order % 2 != 0 %><%= "class='alt'" %><% end %> > <% order += 1 %>
					<td><%= Book.getBookTitle(borrow_book.book_id) %></td>
					<td><%= borrow_book.book_id %></td>
					<td><%= borrow_book.borrowed_time %></td>
					<td><%= borrow_book.return_time %></td>
				</tr>
			<% end %>
			</table>
		<% end %>
	</div><!-- End .list-borrowing-book -->
	
	<% if session[:punished_books] %>
		<h1 style='margin-top: 15px;'>Punishment</h1>
		<div class='punishment'>
			<table>
				<tr>
					<th>Book Name</th>
					<th>Punished Money</th>
				</tr>
				<% @punished_books = session[:punished_books] %>
				<% 
					order = 0
					total = 0
				%>
				<% for punished_book in @punished_books %>
				<tr <% if order % 2 != 0 %><%= "class='alt'" %><% end %> > <% order += 1 %>
					<td><%= punished_book.book.title %></td>
					<td><%= punished_book.money %>&nbsp;VND</td>
				</tr>
				<% total = total + punished_book.money.to_i %> 
				<% end %>
				<tr>
					<td style='text-align: right; padding-right: 15px;'>Total</td>
					<td><%= total %>&nbsp;VND</td>
				</tr>
			</table>
		</div><!-- End .list-borrowing-book -->
	<% end %>
	
	<% if session[:borrowing_books] or session[:punished_books] %>
		<%= form_tag(:controller => 'admin/book_borrow', :action => 'agree_or_clear') do  %>
			<p><%= submit_tag 'Clear', :name => 'clear' %></p>
		<% end %>
	<% end %>
	
	<% if flash[:borrow_notice] != nil %>
		<div class='notice'>
			<h3><%= flash[:borrow_notice] %></h3>
		</div>
	<% end %>
</div><!-- End .return-book -->
